package thread;

/**
 * @User: vitobo
 * @Date: 2024-07-25
 * @Description: 等待线程
 *
 */
public class ThreadDemo10 {


    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            for (int i = 0; i < 3; i++) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        t.start();
        try{
            Thread.sleep(5000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }

        // 本身执行完start之后,  t线程和main线程就并发执行, 分头行动
        // main继续往下执行, t也会继续往下执行;
        // 一直阻塞到 t线程执行结束, main线程才会从join中恢复回来, 才能继续往下执行;

        System.out.println("join 之前");
        t.join();

        System.out.println("join 之后");
        // 如果执行join的时候, t已经结束了, join不会阻塞, 就会立即返回了
    }
}
